// pagesDoctor/prescribing/index.js
import {
  ApisubmitDoctorAdvice,
  ApiGetOrderInfo,
  ApiCaseHistoryAdd
} from '../../api/docIndex'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    medicalHistoryArr: [{
        text: "现病史",
        code: "1",
      },
      {
        text: "月经史",
        code: "2",
      },
      {
        text: "流产史",
        code: "3",
      },
      {
        text: "过敏史",
        code: "4",
      },
      {
        text: "既往史",
        code: "5",
      },
      {
        text: "家族史",
        code: "6",
      },
      {
        text: "手术外伤史",
        code: "7",
      },
      {
        text: "传染病接触史",
        code: "8",
      },
      {
        text: "流行病学史",
        code: "9",
      },
    ],
    opt: {
      PatientHistory: [{
        medicalHistoryType: '5',
        medicalHistoryTypeStr: '既往史',
        describe: ''
      }, {
        medicalHistoryType: '4',
        medicalHistoryTypeStr: '过敏史',
        describe: ''
      }, {
        medicalHistoryType: '1',
        medicalHistoryTypeStr: '现病史',
        describe: ''
      }]
    },
    autosize: {
      maxHeight: 100,
      minHeight: 50
    },
  },


  openClick() {
    wx.navigateTo({
      url: '/pagesDoctor/diseaseSelection/index',
    })
  },
  //新增病例类型
  addPat() {
    this.data.opt.PatientHistory.push({
      medicalHistoryType: '',
      describe: ''
    })
    this.setData({
      'opt.PatientHistory': this.data.opt.PatientHistory
    })
  },


  delPat(e) {
    let index = e.currentTarget.dataset.v
    this.data.opt.PatientHistory.splice(index, 1)
    this.setData({
      'opt.PatientHistory': this.data.opt.PatientHistory
    })
  },


  //切换change
  historyChange(e) {
    let type = e.currentTarget.dataset.v
    let index = e.currentTarget.dataset.index
    this.setData({
      [`opt.PatientHistory[${index}].${type}`]: e.detail
    })
  },
  //病史类型查询
  medicalHistoryTypeClick(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      usageshowpopup: true,
      medicalHistoryIndex: index
    })
  },
  //病史类型选择
  medicalHistoryconfirmBtn(e) {
    this.setData({
      [`opt.PatientHistory[${this.data.medicalHistoryIndex}].medicalHistoryTypeStr`]: e.detail.value.text,
      [`opt.PatientHistory[${this.data.medicalHistoryIndex}].medicalHistoryType`]: e.detail.value.code,
      usageshowpopup: false
    })
  },

  usageonClose() {
    this.setData({
      usageshowpopup: false
    })
  },

  //开药



  //确定按钮
  submitBtn() {

    if (!this.data.opt.DiagnosisName) {
      wx.showToast({
        title: '请选择诊断疾病',
        icon: 'error'
      })
      return
    }
    if (!this.data.opt.PatientComplains) {
      wx.showToast({
        title: '请输入主诉',
        icon: 'error'
      })
      return
    }
    this.setData({
      btnLoading: true
    })
    this.data.opt.PatientHistory = JSON.stringify(this.data.opt.PatientHistory)
    ApiCaseHistoryAdd(this.data.opt).then(res => {
      if (res.code == '200') {
        wx.showToast({
          title: "添加成功",
        })
        this.setData({
          btnLoading: false
        })
      }
      setTimeout(() => {
        wx.navigateBack()
      }, 2000);
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      'opt.order_id': options.orderId,
      'opt.patient_gender': options.patient_gender
    })
  },

  //导入主要诉讼信息
  importData() {
    ApiGetOrderInfo(this.data.opt.order_id).then(res => {
      this.setData({
        'opt.PatientComplains': res.data.patient_status
      })
    })
  },
  //婚姻状态切换
  onChangeMaritalStatus(e) {
    this.setData({
      'opt.MaritalStatus': e.detail
    })
  },
  //病史是否存在切换
  onChangeisexittalStatus(e) {
    this.setData({
      'opt.is_exist': e.detail
    })
  },

  onChange(e) {
    let type = e.currentTarget.dataset.v
    this.setData({
      [`opt.${type}`]: e.detail
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data?.disease) {
      this.setData({
        'opt.DiagnosisName': currPage.data.disease,
        'opt.DiagnosisCode': currPage.data.disease_code,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})